﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{

    public class Cls_ChannelPropertyList : BaseNode
    {
        public Cls_ChannelPropertyList(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        private List<Cls_ChannelProperty> _list = null;

        public List<Cls_ChannelProperty> ChannelPropertys
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                Cls_ChannelProperty c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_ChannelProperty>();

                foreach (XmlNode x in _XmlNode.SelectNodes("ChannelProperty"))
                {
                    c = new Cls_ChannelProperty(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_Channel);
                //}
                _list.Sort();
                return _list;
            }
        }

        public Cls_ChannelProperty ChannelProperty(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("ChannelProperty[@id=" + id.ToString() + "]");
            if (ch == null) return null;

            return new Cls_ChannelProperty(ch);
        }

        public Cls_ChannelProperty ChannelProperty(string name)
        {
            XmlNode ch = null;
            XmlDocument doc = this._XmlNode.OwnerDocument;


            XmlNodeList cmd = _XmlNode.SelectNodes("ChannelProperty");
            foreach (XmlNode xn in cmd)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe["Name"].InnerText == name)
                {
                    ch = xn;
                }
            }
            //add new task  
            //ch = _XmlNode.SelectSingleNode("ChannelProperty[@Name=" + name + "]");
            if (ch == null) return null;
            return new Cls_ChannelProperty(ch);
        }

        public void Sort()
        {

        }

        public void InitChannelPropertys()
        {
            //find 
            XmlDocument doc = this._XmlNode.OwnerDocument;
            for (int i = 0; i < 11; i++)
            {
                XmlNode ch = doc.CreateElement("ChannelProperty");
                ch.Attributes.Append(doc.CreateAttribute("id")).Value = i.ToString();
                ch.AppendChild(doc.CreateElement("Name")).InnerText = "";
                ch.AppendChild(doc.CreateElement("WarningYellowLine")).InnerText = "0";
                ch.AppendChild(doc.CreateElement("AlarmRedline")).InnerText = "0";
                ch.AppendChild(doc.CreateElement("IsUser")).InnerText = "0";
                _XmlNode.AppendChild(ch);
                new Cls_ChannelProperty(ch);
            }
            UpdateChannelPropertyName(0, "PK2PK");
            UpdateChannelPropertyName(1, "GapVoltage");
            UpdateChannelPropertyName(2, "RoomSquareValue");
            UpdateChannelPropertyName(3, "OneMultiplierAmp");
            UpdateChannelPropertyName(4, "OneMultiplierPhase");
            UpdateChannelPropertyName(5, "TWOMultiplierAmp");
            UpdateChannelPropertyName(6, "TWOMultiplierPhase");
            UpdateChannelPropertyName(7, "HalfMultiplierAmp");
            UpdateChannelPropertyName(8, "FrequencyRange1");
            UpdateChannelPropertyName(9, "FrequencyRange2");
            UpdateChannelPropertyName(10, "FrequencyRange3");

        }



        public void UpdateChannelProperty(string Name, string WarningYellowLine, string AlarmRedline,string IsUser)
        {
            Cls_ChannelProperty ch = this.ChannelProperty(Name);
            if (null == ch) return;
            ch.Node["WarningYellowLine"].InnerText = WarningYellowLine.ToString();
            ch.Node["AlarmRedline"].InnerText = AlarmRedline.ToString();
            ch.Node["IsUser"].InnerText = IsUser.ToString();
        }


        public string[] GetChannelProperty(string Name)
        {
            string[] datas = new string[3] { "", "","" };
            Cls_ChannelProperty ch = this.ChannelProperty(Name);
            if (null != ch)
            {
                datas[0] = ch.Node["WarningYellowLine"].InnerText;
                datas[1] = ch.Node["AlarmRedline"].InnerText;
                datas[2] = ch.Node["IsUser"].InnerText;
            }
            return datas;
        }

        void UpdateChannelPropertyName(int id, string NewName)
        {
            Cls_ChannelProperty ch = this.ChannelProperty(id);
            if (null == ch) return;
            ch.Node["Name"].InnerText = NewName;
        }
    }


    public class Cls_ChannelProperty : BaseNode
    {

        public Cls_ChannelProperty(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        public double WarningYellowLine
        {
            get
            {
                if (_XmlNode["WarningYellowLine"] != null)
                {
                    return double.Parse(_XmlNode["WarningYellowLine"].InnerText);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["WarningYellowLine"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("WarningYellowLine"));
                _XmlNode["WarningYellowLine"].InnerText = value.ToString();
            }
        }

        public double AlarmRedline
        {
            get
            {
                if (_XmlNode["AlarmRedline"] != null)
                {
                    return double.Parse(_XmlNode["AlarmRedline"].InnerText);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["AlarmRedline"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("AlarmRedline"));
                _XmlNode["AlarmRedline"].InnerText = value.ToString();
            }
        }

        public int IsUser
        {
            get
            {
                if (_XmlNode["IsUser"] != null)
                {
                    return int.Parse(_XmlNode["IsUser"].InnerText);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["IsUser"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("IsUser"));
                _XmlNode["IsUser"].InnerText = value.ToString();
            }
        }
    }
}
